/*     */ package com.gillion.grule.client.utils;
/*     */ 
/*     */ import com.google.common.base.Optional;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
/*     */ import org.apache.commons.lang3.builder.ToStringStyle;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.BeansException;
/*     */ import org.springframework.beans.factory.InitializingBean;
/*     */ import org.springframework.context.ApplicationContext;
/*     */ import org.springframework.context.ApplicationContextAware;
/*     */ import org.springframework.context.ApplicationEvent;
/*     */ import org.springframework.context.ApplicationListener;
/*     */ import org.springframework.context.event.ContextRefreshedEvent;
/*     */ import org.springframework.context.support.AbstractRefreshableApplicationContext;
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ContextHolder implements ApplicationContextAware, InitializingBean, ApplicationListener<ContextRefreshedEvent>
/*     */ {
/*     */   public static ApplicationContext appContext;
/*  24 */   private static final Logger CONTEXT_LOGGER = LoggerFactory.getLogger(ContextHolder.class);
/*     */   
/*     */   public static final <T> T getBean(Class<T> requireType) {
/*  27 */     return (T)appContext.getBean(requireType);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static <T> T getBean(String beanName) {
/*  39 */     if (appContext == null) {
/*  40 */       CONTEXT_LOGGER.error("applicationContext不能为空！");
/*  41 */       return null;
/*     */     } 
/*  43 */     if (StringUtils.isEmpty(beanName)) {
/*  44 */       CONTEXT_LOGGER.error("bean名称不能为空！");
/*  45 */       return null;
/*     */     } 
/*  47 */     Object bean = null;
/*     */     try {
/*  49 */       bean = appContext.getBean(beanName);
/*  50 */     } catch (Exception e) {
/*  51 */       CONTEXT_LOGGER.error("获取spring bean[" + beanName + "]错误：" + e.getMessage(), e);
/*  52 */       return null;
/*     */     } 
/*  54 */     if (bean == null) {
/*  55 */       CONTEXT_LOGGER.error("找不到ID为[" + beanName + "]的spring bean实现，请检查spring配置。");
/*  56 */       return null;
/*     */     } 
/*  58 */     return (T)bean;
/*     */   }
/*     */   
/*     */   public static final <T> Optional<T> getOptionalBean(String beanName) {
/*     */     try {
/*  63 */       T bean = (T) appContext.getBean(beanName);
/*  64 */       return Optional.of(bean);
/*  65 */     } catch (BeansException e) {
/*  66 */       CONTEXT_LOGGER.error("获取spring bean[" + beanName + "]出错", (Throwable)e);
/*     */       
/*  68 */       return Optional.absent();
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static final String resolveEmbeddedValue(String propName) {
/*  78 */     return ((AbstractRefreshableApplicationContext)appContext).getBeanFactory().resolveEmbeddedValue(propName);
/*     */   }
/*     */ 
/*     */   
/*     */   public void afterPropertiesSet() throws Exception {
/*  83 */     ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.MULTI_LINE_STYLE);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void setApplicationContext(ApplicationContext context) {
/*  96 */     appContext = context;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void onApplicationEvent(ContextRefreshedEvent event) {
/* 102 */     CONTEXT_LOGGER.info("holded applicationContext,displayName");
/*     */   }
/*     */ }


/* Location:              D:\apache-tomcat-9.0.76\webapps\grule-manager\WEB-INF\lib\grule-client-2.0.2.RELEASE.jar!\com\gillion\grule\clien\\utils\ContextHolder.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */